package com.cheung.leetcode.binarysearch;

public class LC278 {

    public static int firstBadVersion(int n) {
        int left = 0;
        int right = n;
        while (left + 1 != right) {
            // （left+right）/2 可能溢出
            int mid = left + (right - left) / 2;
            if (isBadVersion(mid)) {
                right = mid;
            } else {
                left = mid;
            }
        }

        return right;
    }

    static boolean isBadVersion(int version) {
        return version >= 2147483647;
    }

    public static void main(String[] args) {
        System.out.println(firstBadVersion(2147483647));
    }
}
